package com.lhd.controller;

import com.lhd.entity.dto.UserDto;
import com.lhd.entity.enums.PageSize;
import com.lhd.entity.enums.ResponseCodeEnum;
import com.lhd.entity.po.Course;
import com.lhd.entity.po.UserCourse;
import com.lhd.entity.query.CourseQuery;
import com.lhd.entity.query.UserCourseQuery;
import com.lhd.entity.vo.PaginationResultVO;
import com.lhd.entity.vo.ResponseVO;
import com.lhd.exception.BusinessException;
import com.lhd.service.CourseService;
import com.lhd.service.UserCourseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: liuhd
 * @Date: 2025/03/10
 * @Description: Course控制器
 */
@RestController
@RequestMapping("/course")
public class CourseController extends ABaseController{

	@Resource
	private CourseService courseService;
	@Resource
	private UserCourseService userCourseService;

	/**
	 * @description: 获取所在课程信息
	 * @param pageNo 页号，可选，默认为1
	 * @param pageSize 页大小，可选，默认为15
	 * @return com.lhd.entity.vo.ResponseVO
	 * @author liuhd
	 * 2025/4/29 16:55
	 */
	@GetMapping("/list")
	public ResponseVO getCourseList(@RequestParam(required = false) Integer pageNo,@RequestParam(required = false) Integer pageSize){
		if (pageNo == null || pageNo == 0){
			pageNo = 1;
		}
		if (pageSize == null || pageSize <= 0){
			pageSize = PageSize.SIZE15.getSize();
		}
		Integer uid = getUserInfoToken().getId();

		// 查出所在的课程
		UserCourseQuery userCourseQuery = new UserCourseQuery();
		userCourseQuery.setUid(uid);
		userCourseQuery.setPageSize(Long.valueOf(pageSize));
		userCourseQuery.setPageNo(Long.valueOf(pageNo));
		PaginationResultVO<UserCourse> listByPage = userCourseService.findListByPage(userCourseQuery);

		if (listByPage.getResultCount() == 0) return getSuccessResponseVO(null);

		List<Integer> cidList = new ArrayList<>();
		for (UserCourse userCourse : listByPage.getList()) {
			Integer cid = userCourse.getCid();
			cidList.add(cid);
		}

		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setCidList(cidList);
		List<Course> courseList = courseService.findListByQuery(courseQuery);

		PaginationResultVO<Course> paginationResultVO = new PaginationResultVO<>();
		paginationResultVO.setPageNo(listByPage.getPageNo());
		paginationResultVO.setPageSize(listByPage.getPageSize());
		paginationResultVO.setPageTotal(listByPage.getPageTotal());
		paginationResultVO.setResultCount(listByPage.getResultCount());
		paginationResultVO.setList(courseList);

		return getSuccessResponseVO(paginationResultVO);
	}

	@GetMapping("/{cid}")
	public ResponseVO getCourse(@PathVariable Integer cid){
//		Integer uid = getUserInfoToken().getId();
		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setId(cid);
		List<Course> courseList = courseService.findListByQuery(courseQuery);
		if (courseList.size() > 1) throw new BusinessException(ResponseCodeEnum.CODE_600);
		Course course = courseList.get(0);
		return getSuccessResponseVO(course);
	}

	@PostMapping("/add")
	public ResponseVO addCourse(@RequestBody Course course){
		Integer teacherId = getUserInfoToken().getId();
		if (course.getId() != null){
			throw new BusinessException("增加课程不能传递课程id!");
		}
		course.setUid(teacherId);
		courseService.add(course);
		return getSuccessResponseVO(null);
	}

	@PutMapping("/{cid}")
	public ResponseVO modifyCourse(@PathVariable Integer cid,@RequestBody Course course){
		Integer teacherId = getUserInfoToken().getId();
		if (course.getId() != null){
			throw new BusinessException("增加课程不能传递课程id!");
		}
		if (course.getUid() != null){
			throw new BusinessException("增加课程不能传递创建者id!");
		}
		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setUid(teacherId);
		courseQuery.setId(cid);
		courseService.updateByQuery(course,courseQuery);
		return getSuccessResponseVO(null);
	}

	@DeleteMapping("/{cid}")
	public ResponseVO removeCourse(@PathVariable Integer cid){
		courseService.deleteCourseById(cid);
		return getSuccessResponseVO(null);
	}
}